<?php

require_once('Vega/Dao/Table.php');

class Mobitv_Admin_VideocatDao extends Vega_Dao_Table {

    public function getList($showAll = false, $catagoryId = '') {
        if ($showAll)
            $sql = "SELECT * FROM clip_category WHERE 1=1 ";
        else
            $sql = "SELECT * FROM clip_category WHERE visible in (1,3) ";
        if ($catagoryId != '') {
            $catagoryId = intval($catagoryId);
            $sql .= " AND id <> $catagoryId ";
        }
        $sql .= " ORDER BY `order`";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function getAllVodSpecialCategory($visible = 3) {
        $sql = "SELECT * FROM clip_category WHERE visible=$visible ";
        $sql .= " ORDER BY `order`";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function getInvisibleList($catagoryId = '') {
        $sql = "SELECT * FROM clip_category WHERE visible=0 ";
        if ($catagoryId != '') {
            $sql .= " AND id <> $catagoryId ";
        }
        $sql .= " ORDER BY `order`";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function isInvisible($catId) {
        $catId = intval($catId);

        $sql = "SELECT visible FROM clip_category WHERE id=$catId";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();

        return $row ? $row['visible'] == 0 : false;
    }

    public function getParentList() {
        $sql = "SELECT * FROM clip_category WHERE parent_id=0";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function getInvisibleParentList() {
        $sql = "SELECT * FROM clip_category WHERE parent_id=0 AND visible=0";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function getDetail($catId) {
        $catId = intval($catId);
        $sql = "SELECT * FROM clip_category WHERE id=$catId";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }

    public function getMaxOrder() {
        $sql = "SELECT max(`order`) AS 'max' FROM clip_category WHERE parent_id=0";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();

        return $row ? $row['max'] : 0;
    }

    public function countAll() {
        $sql = "SELECT max(`order`) AS 'count' FROM clip_category";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();

        return $row ? $row['count'] : 0;
    }

    public function insert($data) {
        $name = $this->_db->valueQuote($data['name']);
        $name_vn = $this->_db->valueQuote($data['name_vn']);
        $parentId = intval($data['parent_id']);
        $isHot = intval($data['is_hot']);
        $visible = intval($data['visible']);
        $price = floatval($data['price']);
        $price_retail = floatval($data['price_retail']);
        $cp_id = intval($data['cp_id']);
        $sql = "INSERT INTO clip_category SET
                    name=$name
                    ,name_vn=$name_vn
                    ,parent_id=$parentId
                    ,is_hot=$isHot
                    ,visible=$visible
                    ,price = $price
                    ,price_retail = $price_retail
                    ,cp_id = $cp_id
                    ,updated_time=now()";
        //echo $sql; exit;
        $result = $this->_db->queryWrite($sql);
        if ($result)
            $this->reOrder();
        return $result;
    }

    public function save($data, $catId) {
        $name = $this->_db->valueQuote($data['name']);
        $name_vn = $this->_db->valueQuote($data['name_vn']);
        $parentId = intval($data['parent_id']);
        $isHot = intval($data['is_hot']);
        $visible = intval($data['visible']);
        $price = floatval($data['price']);
        $price_retail = floatval($data['price_retail']);
        $cp_id = intval($data['cp_id']);
        $sql = "UPDATE clip_category SET name=$name, name_vn=$name_vn, cp_id=$cp_id, parent_id=$parentId, is_hot=$isHot, visible=$visible, price = $price, price_retail = $price_retail, updated_time=now() WHERE id=$catId";
        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list) {
        $list = "(" . implode(",", $list) . ")";
        $sql = "DELETE FROM clip_category WHERE id IN $list";
        $result = $this->_db->queryWrite($sql);
        //Reorder
        if ($result)
            $this->reOrder();
        return $result;
    }

    public function getLastInsertId() {
        return $this->_db->getLastInsertId();
    }

    public function updateVideoCatThumbnail($catId) {
        $sql = "UPDATE clip_category SET thumbnail_version=thumbnail_version + 1
                WHERE id = " . $catId;
        return $this->_db->queryWrite($sql);
    }

    /*
     * Hàm thay đổi (di chuyển) thứ tự hiển thị của một chuyên mục
     *
     */

    public function move($videocatId, $curOrder, $newOrder, $maxOrder) {
        // Chuyen len dau danh sach
        if ($newOrder == 1) {
            $sql = "UPDATE clip_category SET `order`=`order` + 1 WHERE parent_id=0 AND `order` < $curOrder";
            $this->_db->queryWrite($sql);

            $sql = "UPDATE clip_category SET `order`=$newOrder WHERE id=$videocatId";
            return $this->_db->queryWrite($sql);
        } elseif ($newOrder == $maxOrder) {
            $sql = "UPDATE clip_category SET `order`=`order` - 1 WHERE parent_id=0 AND `order` > $curOrder";
            $this->_db->queryWrite($sql);

            $sql = "UPDATE clip_category SET `order`=$newOrder WHERE id=$videocatId";
            return $this->_db->queryWrite($sql);
        } elseif ($newOrder == $curOrder + 1 ||
                $newOrder == $curOrder - 1
        ) {//chuyển xuống dưới | lên trên
            $sql = "UPDATE clip_category SET `order`=$curOrder WHERE `order`=$newOrder";
            $this->_db->queryWrite($sql);

            $sql = "UPDATE clip_category SET `order`=$newOrder WHERE id=$videocatId";
            return $this->_db->queryWrite($sql);
        } else {
            return true;
        }
    }

    /*
     * Hàm sắp xếp lại thứ tự hiển thị chuyên mục
     */

    public function reOrder() {
        $sql = "SELECT id FROM clip_category WHERE parent_id=0 ORDER BY `order` ASC, id DESC";
        $this->_db->query($sql);
        $rows = $this->_db->fetchRowSet();
        $index = 1;
        $ret = true;
        foreach ($rows as $row) {
            $id = $row['id'];
            $sql = "UPDATE clip_category SET `order`=$index WHERE id=$id";
            if (!($this->_db->queryWrite($sql)))
                $ret = false;
            $index++;
        }
        return $ret;
    }

    public function transferVideoCat($fromId, $toId) {
        $sql = "SELECT count(*) total FROM clip WHERE category_id = $fromId";
        $this->_db->query($sql);
        $result = $this->_db->fetchRow();
        if ($result['total'] > 0) {
            $sql = "UPDATE clip SET category_id = $toId WHERE category_id = $fromId";
            return $this->_db->queryWrite($sql);
        } else {
            return true;
        }
    }

}